#region using

using System;
using System.Web;
using System.Web.UI;
using BlogEngine.Core.Web.Controls;
using BlogEngine.Core;

#endregion

/// <summary>
/// Breaks a post where [more] is found in the body and adds a link to full post.
/// </summary>
[Extension("Adds a DiggThis button to every post", "1.2", "Joe Kaiser")]
public class RedditIt
{

    /// <summary>
    /// Hooks up an event handler to the Post.Serving event.
    /// </summary>
    public RedditIt()
    {
        Post.Serving += new EventHandler<ServingEventArgs>(Post_Serving);
    }


    /// <summary>
    /// Handles the Serving event of the Post control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="BlogEngine.Core.ServingEventArgs"/> instance containing the event data.</param>
    private void Post_Serving(object sender, ServingEventArgs e)
    {
        if (e.Location != ServingLocation.Feed)
        {
            AddButton(sender, e);
        }
    }


    /// <summary>
    /// Adds the button.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="BlogEngine.Core.ServingEventArgs"/> instance containing the event data.</param>
    private static void AddButton(object sender, ServingEventArgs e)
    {
        Post p = sender as Post;
        if (p != null)
        {
            string script = string.Format("<script>reddit_url='{0}'; reddit_title='{1}'</script><script language='javascript' src='http://reddit.com/button.js?t=3'></script>", p.AbsoluteLink, p.Title);

            e.Body = String.Format("{0}<br /><br />{1}", e.Body, script);
        }
    }
}
